package com.ease.business.importing.vo;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.ease.actionscript.Remoting;
import com.ease.common.util.Assertion;
import com.ease.document.poi.WorkbookUtil;

/**
 * 
 * @author nathanleewei
 * 
 */
@Remoting(false)
public class WorkbookDataGridSource implements Serializable, DataGridSource<HSSFWorkbook> {

	/**
	 *
	 */
	private static final long serialVersionUID = 8378716519048751789L;

	protected String fileName;

	protected String fileSuffixName;

	protected byte[] fileContent;

	protected HSSFWorkbook workbook;

	private int maxRows = 1000;

	public WorkbookDataGridSource() {

	}

	public WorkbookDataGridSource(String fileName, String fileSuffixName, byte[] fileContent) {

		this.setFileName(fileName);
		this.setFileSuffixName(fileSuffixName);
		this.setFileContent(fileContent);

		init();

	}

	public void init() {

		InputStream fis = new ByteArrayInputStream(this.getFileContent());

		POIFSFileSystem fs = null;
		try {
			fs = new POIFSFileSystem(fis);
			workbook = new HSSFWorkbook(fs);
		} catch (IOException e) {
			if ("Unable to construct record instance".equals(e.getMessage())) {
				throw new RuntimeException("不支持带有数据有效性校验的Excel文件", e);
				// throw new RuntimeException("不支持带有数据有效性校验的Excel文件");
			} else {
				throw new RuntimeException(e);
			}
		}

		HSSFSheet importSheet = workbook.getSheetAt(0);
		Assertion.notNull(importSheet, "Excel's sheet is null");
		// Assertion.notNull(importSheet.getRow(0), "There is no data in the sheet");

	}

	private HSSFSheet getRawGrid() {
		if (null != workbook) {
			return workbook.getSheetAt(0);
		}
		return null;
	}

	public HSSFRow getRawRow(int rowIndex) {
		HSSFSheet sheet = getRawGrid();
		if (null != sheet) {
			return sheet.getRow(rowIndex);
		}
		return null;
	}

	public int getRowSize() {
		HSSFSheet sheet = getRawGrid();
		if (null != sheet) {
			return sheet.getLastRowNum();
		}
		return -1;
	}

	public int getColumnSize() {
		HSSFRow row = WorkbookUtil.firstRowOnFirstSheet(workbook);
		return row.getPhysicalNumberOfCells();
	}

	public DataGridSourceHeader<HSSFWorkbook> getHeader() {
		return null;
	}

	public List<Object> getRow(int rowIndex) {
		HSSFSheet sheet = getRawGrid();
		if (null == sheet) {
			return null;
		}
		HSSFRow headerRow = sheet.getRow(rowIndex);
		if (null == headerRow) {
			throw new RuntimeException("找不到行=" + rowIndex);
		}

		int rawColumnCount = headerRow.getPhysicalNumberOfCells();
		List<Object> list = new ArrayList<Object>();
		for (int i = 0; i < rawColumnCount; i++) {

			HSSFCell cell = headerRow.getCell(i);
			list.add(WorkbookUtil.getCellString(cell));
		}

		return list;
	}

	public List<Object> getColumn(int columnIndex) {

		return null;
	}

	public int getFirstRowIndex() {
		// HSSFSheet sheet = getRawGrid();
		// if (null != sheet) {
		// return sheet.getFirstRowNum();
		// }
		return 1;
	}

	public Object getCell(int rowIndex, int columnIndex) {
		HSSFRow rawRow = getRawRow(rowIndex);
		if (null != rawRow) {
			HSSFCell cell = rawRow.getCell(columnIndex);
			return WorkbookUtil.getCellObject(cell);
		}
		return null;
	}

	/**
	 * @return the maxRows
	 */
	public int getMaxRows() {
		return maxRows;
	}

	/**
	 * @param maxRows
	 *            the maxRows to set
	 */
	public void setMaxRows(int maxRows) {
		this.maxRows = maxRows;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param fileName
	 *            the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * @return the fileSuffixName
	 */
	public String getFileSuffixName() {
		return fileSuffixName;
	}

	/**
	 * @param fileSuffixName
	 *            the fileSuffixName to set
	 */
	public void setFileSuffixName(String fileSuffixName) {
		this.fileSuffixName = fileSuffixName;
	}

	/**
	 * @return the fileContent
	 */
	public byte[] getFileContent() {
		return fileContent;
	}

	/**
	 * @param fileContent
	 *            the fileContent to set
	 */
	public void setFileContent(byte[] fileContent) {
		this.fileContent = fileContent;
	}

}
